home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / DBINFO.C < prev    next >
Text File  |  1985-10-29  |  5KB  |  238 lines

  1. /*
  2.  * Program to read a dBASE III .DBF file, and display the header information.
  3.  */
  4. #include "stdio.h"
  5. main(argc,argv)
  6.     int argc;
  7.     char *argv[];
  8.     {
  9.     char ftype[10],
  10.      noext;
  11.     int  c,
  12.      cntr,
  13.      reclen,
  14.      reccntr,
  15.      records,
  16.      numread,
  17.      outfile,
  18.      inlen,
  19.      infile;
  20.     char infname[64],
  21.      exfname[64],
  22.      inext[5];
  23.     struct db3head
  24.     {
  25.     /* byte 1 */
  26.     char version;
  27.     /* bytes 2-4 */
  28.     char year,
  29.          month,
  30.          day;
  31.     /* bytes 5-6 */
  32.     char lorecs,
  33.          hirecs;
  34.     /* bytes 7-8 */
  35.     char dummyh01[2];
  36.     /* bytes 9-10 */
  37.     char datadisp[2];
  38.     /* bytes 11-12 */
  39.     char recdlen[2];
  40.     /* bytes 13-32 */
  41.     char dummyh02[20];
  42.     };
  43.     struct db3rec
  44.     {
  45.     /* bytes 1-11 - NB 11th byte always a null */
  46.     char f_name[11];
  47.     /* byte 12 */
  48.     char f_type;
  49.     /* bytes 13-14 */
  50.     char dummyf01[2];
  51.     /* bytes 15-16 - seems to always be 0x3C56 or 3E56 */
  52.     char dummyf02[2];
  53.     /* byte 17 */
  54.     char f_len;
  55.     /* byte 18 */
  56.     char f_dec;
  57.     /* bytes 19-32 */
  58.     char dummyf03[14];
  59.     };
  60.     struct db3head header;
  61.     struct db3rec  field;
  62.     if ((argc == 1) || (argv[1][0] == '?'))
  63.     {
  64.     puts("\n");
  65.     puts("Syntax: DBINFO <infile> [<outfile>]\n");
  66.     puts("where   infile  is the filespec for the input data.\n");
  67.     puts("        outfile is the filespec for the file to which\n");
  68.     puts("        the output is to be appended.\n");
  69.     puts("\n");
  70.     puts("The input file - a dBASE III database file - is read and\n");
  71.     puts("the file header information displayed to simulate the\n");
  72.     puts("dBASE command DISPLAY STRUCTURE.\n");
  73.     puts("\n");
  74.     puts("An extension of .DBF is assumed for the input file if\n");
  75.     puts("none is given.\n");
  76.     puts("\n");
  77.     puts("Only extensions of .DBF and .BAK are valid input file\n");
  78.     puts("extensions.\n");
  79.     puts("\n");
  80.     puts("If no output file is given, the output is routed by default\n");
  81.     puts("to the screen.\n");
  82.     puts("\n");
  83.     exit(0);
  84.     }
  85.     if (argv[1][0] == '#')
  86.     {
  87.     puts("\n");
  88.     puts("PROGRAM: DBINFO\n");
  89.     puts("Author : Peter Townsend\n");
  90.     puts("Date   : 28Oct85\n");
  91.     puts("Version: 1.1\n");
  92.     exit(0);
  93.     }
  94.     inlen = strlen(argv[1]);
  95.     for(cntr = 0;cntr <= inlen;cntr++)
  96.     {
  97.     infname[cntr] = toupper(argv[1][cntr]);
  98.     }
  99.     if ((infname[inlen-4] != '.') || (inlen < 4))
  100.     {
  101.     strcat(infname,".DBF");
  102.     }
  103.     inlen = strlen(infname);
  104.     for(cntr = inlen-4;cntr <= inlen;cntr++)
  105.     {
  106.     inext[cntr-inlen+4] = infname[cntr];
  107.     }
  108.     if ((strcmp(inext,".DBF") != 0) && (strcmp(inext,".BAK") != 0))
  109.     {
  110.     printf("\nInvalid extension ... must be either .DBF or .BAK\n");
  111.     exit(1);
  112.     }
  113.     if ((infile = open(infname,0)) == -1)
  114.     {
  115.     printf("\nCannot open input file %s\n",infname);
  116.     exit(1);
  117.     }
  118.     if (argc == 2)
  119.     {
  120.     strcpy(exfname,"CON");
  121.     }
  122.     else
  123.     {
  124.     strcpy(exfname,argv[2]);
  125.     }
  126.     if (strcmp(infname,exfname) == 0)
  127.     {
  128.     printf("\nInput filename and output filename cannot be the same.\n");
  129.     exit(1);
  130.     }
  131.     if ((outfile = fopen(exfname,"a")) == -1)
  132.     {
  133.     if ((outfile = creat(exfname)) == -1)
  134.         {
  135.         printf("\nCannot open/create output file %s\n",exfname);
  136.         exit(1);
  137.         }
  138.     }
  139.     numread = read(infile,header,32);
  140.     if (numread != 32)
  141.     {
  142.     printf("\nCannot access file header in database\n");
  143.     exit(1);
  144.     }
  145.     c = header.version % 0x10;
  146.     if (c != 3)
  147.     {
  148.     printf("\nNot a dBASE III database\n");
  149.     exit(1);
  150.     }
  151.     c = header.version / 0x10;
  152.     if ((c != 8) && (c != 0))
  153.     {
  154.     printf("\nNot a dBASE III database\n");
  155.     exit(1);
  156.     }
  157.     records = header.lorecs + (header.hirecs * 16);
  158.     fprintf(outfile,"\n");
  159.     fprintf(outfile,"Structure for database : %s\n",infname);
  160.     fprintf(outfile,"Number of data records :   %5d\n",records);
  161.     fprintf(outfile,"Date of last update    : ");
  162.     fprintf(outfile,"%02d/%02d/%02d\n",header.month,header.day,header.year);
  163.     reccntr = 1;
  164.     fprintf(outfile,"Field  Field name  Type        Width    Dec\n");
  165.     numread = read(infile,field,32);
  166.     if (numread != 32)
  167.     {
  168.     fprintf(outfile,"Cannot access next field\'s data\n");
  169.     exit(1);
  170.     }
  171.     reclen = 1;
  172.     while (isprint(field.f_name[0]))
  173.     {
  174.     if (field.f_type == 'C')
  175.         {
  176.         strcpy(ftype,"Character");
  177.         }
  178.     else
  179.         {
  180.         if (field.f_type == 'N')
  181.         {
  182.         strcpy(ftype,"Numeric");
  183.         }
  184.         else
  185.         {
  186.         if (field.f_type == 'L')
  187.             {
  188.             strcpy(ftype,"Logical");
  189.             }
  190.         else
  191.             {
  192.             if (field.f_type == 'D')
  193.             {
  194.             strcpy(ftype,"Date");
  195.             }
  196.             else
  197.             {
  198.             if (field.f_type == 'M')
  199.                 {
  200.                 strcpy(ftype,"Memo");
  201.                 }
  202.             else
  203.                 {
  204.                 strcpy(ftype,"*Unknown*");
  205.                 }
  206.             }
  207.             }
  208.         }
  209.         }
  210.     reclen = reclen + field.f_len;
  211.     fprintf(outfile,"%5d  %-10s  %-9s  ",reccntr,field.f_name,ftype);
  212.     fprintf(outfile,"%6d",field.f_len);
  213.     if (field.f_dec != 0)
  214.         {
  215.         fprintf(outfile,"  %5d",field.f_dec);
  216.         }
  217.     fprintf(outfile,"\n");
  218.     reccntr++;
  219.     numread = read(infile,field,32);
  220.     if (numread != 32)
  221.         {
  222.         if (records > 0)
  223.         {
  224.         fprintf(outfile,"Cannot access next field\'s data\n");
  225.         exit(1);
  226.         }
  227.         else
  228.         {
  229.         field.f_name[0] = 0;
  230.         }
  231.         }
  232.     }
  233.     fprintf(outfile,"** Total **                   %6d\n",reclen);
  234.     fclose(infile);
  235.     fclose(outfile);
  236.     exit(0);
  237.     }
  238.